package com.zyd.service.setmeal.impl;

import com.zyd.context.HeaderContext;
import com.zyd.io.model.RolePowerEntity;
import com.zyd.io.model.SafeEntity;
import com.zyd.io.model.SafeEntityKey;
import com.zyd.io.page.PageBean;
import com.zyd.io.page.PageRequestBean;
import com.zyd.mappers.SafeEntityMapper;
import com.zyd.service.power.PowerService;
import com.zyd.service.setmeal.SetMealService;
import com.zyd.util.SqlUtils;
import com.zyd.util.ToolUtils;
import com.zyd.util.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Component
public class SetMealServiceImpl implements SetMealService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SetMealServiceImpl.class);

    @Autowired
    private SafeEntityMapper mapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private PowerService powerService;

    @Override
    public PageBean<SafeEntity> querySetMealPage(PageRequestBean request) {
        String countSql = "select count(*)";
        String selectFields = "select * ";
        StringBuilder sql = new StringBuilder();
        sql.append("from zydzn_safe where dr = 0");

        if (request.getString("salename")!=null){
            String salename = request.getString("salename");
            String substring = salename.substring(7);
            String replace = substring.replace(']', '%');
            sql.append(" and salename like '%"+replace+"'");
        }
        if (request.getString("saledesc")!=null){
            String salename = request.getString("saledesc");
            String substring = salename.substring(7);
            String replace = substring.replace(']', '%');
            sql.append(" and saledesc like '%"+replace+"'");
        };

        RolePowerEntity rolePowerEntity = powerService.queryPowerListByUserId((String) HeaderContext.get().get("userid"), "2010");
        if (rolePowerEntity != null && ! org.springframework.util.StringUtils.isEmpty(rolePowerEntity.getArea())) {

            sql.append(" and area in (" + SqlUtils.addIn(rolePowerEntity.getArea()) + ")");
        }
        else{
            return null;
        }
        Long totalRows = jdbcTemplate.queryForObject(countSql + sql.toString(), Long.class);
        String pageSql = SqlUtils.addPage(selectFields + sql.toString(), request.getCurrentPage(), request.getPageSize());

        List<SafeEntity> ret = jdbcTemplate.query(pageSql, new RowMapper<SafeEntity>() {

            @Override
            public SafeEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
                SafeEntity entity = SqlUtils.mappingToBean(rs, SafeEntity.class);
                return entity;
            }
        });

        PageBean<SafeEntity> pageBean = new PageBean<SafeEntity>(request.getCurrentPage(), request.getPageSize(), totalRows.intValue());
        pageBean.setItems(ret);
        return pageBean;
    }

    @Override
    public SafeEntity findMeal(String id) {
        SafeEntityKey key = new SafeEntityKey();
        key.setId(id);
        return mapper.selectByPrimaryKey(key);
    }

    @Override
    public void addMeal(SafeEntity entity) {
        entity.setId(UUIDUtil.getUUid());
        entity.setDr(0);
        entity.setTs(ToolUtils.getNowTime());
        entity.setCreatetime(ToolUtils.getNowTime());
        mapper.insertSelective(entity);
    }

    @Override
    public void updateMeal(SafeEntity entity) {
        mapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteMeal(String id) {
        SafeEntity entity = new SafeEntity();
        entity.setId(id);
        entity.setDr(1);

        mapper.updateByPrimaryKeySelective(entity);
    }
}
